package com.zhz.mall.admin.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.mall.common.api.CommonPage;
import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.admin.dto.ProductAttributeCateDTO;
import com.zhz.mall.admin.modules.pms.model.PmsProductAttributeCategory;
import com.zhz.mall.admin.modules.pms.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author zhouhengzhe
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService attributeCategoryService;

    /**
     * 查询商品分类 列表
     * url:'/productAttribute/category/list',
     * method:'get',
     * params:params
     */
    @RequestMapping("/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<PmsProductAttributeCategory> page = attributeCategoryService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 添加商品类型
     * return request({
     * url:'/productAttribute/category/create',
     * method:'post',
     * data:data
     * })
     */
    @PostMapping("/create")
    public CommonResult<Boolean> create(PmsProductAttributeCategory productAttributeCategory) {

        return attributeCategoryService.add(productAttributeCategory) ? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     * 商品类型修改
     * return request({
     * url:'/productAttribute/category/update/'+id,
     * method:'post',
     * data:data
     * })
     */
    @PostMapping("/update/{id}")
    public CommonResult<Boolean> update(PmsProductAttributeCategory productAttributeCategory) {
        return attributeCategoryService.updateById(productAttributeCategory) ? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     * 商品类型删除
     * url:'/productAttribute/category/delete/'+id,
     * method:'get'
     */
    @GetMapping(value = "/delete/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        return attributeCategoryService.removeById(id) ? CommonResult.success(true) : CommonResult.failed();


    }

    /**
     * 筛选属性下拉级联数据
     * url:'/productAttribute/category/list/withAttr',
     * method:'get'
     */
    @GetMapping("/list/withAttr")
    public CommonResult<List<ProductAttributeCateDTO>> getListWithAttr() {
        return CommonResult.success(attributeCategoryService.getListWithAttr());
    }
}

